package by.grsu.bso.action;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import by.grsu.bso.model.Order;
import by.grsu.bso.model.OrderItem;
import by.grsu.bso.model.ShoppingCartItem;
import by.grsu.bso.model.User;
import by.grsu.bso.service.ServiceManager;

public class DeleteCartItemAction extends Action {

	private final static String SUCCESS = "success";
	private final static String FAILURE = "failure";
	private final static String CART_ITEM_ID ="cart_item_count";

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		String cartOperation = request.getParameter("cart_operation");
		String[] cartItems = request.getParameterValues("cart_items");		

		if (cartOperation.equals("recalculation_cart")) {
			if (cartItems != null) {
				for (String item : cartItems) {
					ServiceManager.getShoppingCartItemService().updateQuantity(Integer.valueOf(item), Integer.valueOf(request.getParameter(CART_ITEM_ID + item)));
				}
			}

		} else if (cartOperation.equals("delete_cart")) {
			if (cartItems != null) {
				for (String item : cartItems) {
					ServiceManager.getShoppingCartItemService().delete(Integer.valueOf(item));
				}
			}

		} 
		
		else if (cartOperation.equals("order_cart")) {
			if (cartItems != null) {
				User user = (User) request.getSession(true).getAttribute("user");
				if ( ((user.getAddress() != null) && (user.getAddress() != "")) && ((user.getTelephone() != null) && (user.getTelephone() != "")) ) {
					
					Date d = new Date();
					SimpleDateFormat formatDate = new SimpleDateFormat("dd.MM.yyyy");
					SimpleDateFormat formatTime = new SimpleDateFormat("hh:mm");
					String orderDate = formatDate.format(d); // date 25.02.2013
					String orderTime = formatTime.format(d); // hour 09:03
					
					Order order = ServiceManager.getOrderService().save(new Order(user.getId(), 1, orderDate, orderTime));
					for (String item : cartItems) {
						ShoppingCartItem cartItemInst = ServiceManager.getShoppingCartItemService().findById(Integer.valueOf(item));
						
						ServiceManager.getOrderItemService().createOrderItem((new OrderItem(
																	order.getId(), 
																	cartItemInst.getProductId(), 
																	cartItemInst.getQuantity() ) ));
						
						ServiceManager.getShoppingCartItemService().delete(Integer.valueOf(item));
					}					
					
				} else {
					response.sendRedirect(request.getContextPath() + "/pages/info_pages/not_enough_data.jsp");
					return mapping.findForward(FAILURE);
				}
			}
			
		}

		response.sendRedirect(request.getContextPath() + "/pages/shopping_cart.jsp");
		return mapping.findForward(SUCCESS);
	}
}
